package com.mmall.service.impl;

import com.google.common.collect.Sets;
import com.mmall.common.ServerResponse;
import com.mmall.dao.CategoryMapper;
import com.mmall.pojo.Category;
import com.mmall.service.ICategoryService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * 商品分类管理逻辑层实现类
 * Created by qezhhnjy on 2017/9/26-16:53.
 */
@Service("iCategoryService")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class CategoryServiceImpl implements ICategoryService {

    private Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public ServerResponse addCategory(String categoryName, Integer parentId) {
        if (parentId == null || StringUtils.isBlank(categoryName)) {
            return ServerResponse.createByErrorMessage("添加品类参数错误!");
        }
        Category category = new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
        //设置该分类为可用
        category.setStatus(true);

        if (categoryMapper.insertSelective(category) > 0)
            return ServerResponse.createBySuccess("添加品类成功!");
        return ServerResponse.createByErrorMessage("添加品类失败!");
    }

    @Override
    public ServerResponse updateName(Integer categoryId, String categoryName) {
        if (categoryId == null || StringUtils.isBlank(categoryName))
            return ServerResponse.createByErrorMessage("更新品类参数错误!");
        Category category = new Category();
        category.setId(categoryId);
        category.setName(categoryName);

        int rowCount = categoryMapper.updateByPrimaryKeySelective(category);
        if (rowCount > 0)
            return ServerResponse.createBySuccessMessage("更新品类名字成功!");
        return ServerResponse.createByErrorMessage("更新品类名字失败!");
    }

    @Override
    public ServerResponse<List<Category>> getChildrenParallel(Integer categoryId) {
        List<Category> categories = categoryMapper.selectChildByParentId(categoryId);
        //工具类判断了该集合是否为null和集合中元素个数是否为空
        if (CollectionUtils.isEmpty(categories))
            logger.info("未找到当前分类的子分类");
        return ServerResponse.createBySuccess(categories);
    }

    //获取全部子分类,如果本身为最底层子分类则,返回它本身
    @Override
    public ServerResponse<Set<Category>> getAllChildren(Integer categoryId) {
        Set<Category> set = Sets.newHashSet();
        getChildRecursion(set, categoryId);
        //如果没有子分类,则判定id是否存在,存在则添加到set中返回.
        if (CollectionUtils.isEmpty(set)) set.add(categoryMapper.selectByPrimaryKey(categoryId));
        return ServerResponse.createBySuccess(set);
    }

    //获取全部最底层子分类,如果本身为最底层子分类则,返回它本身
    @Override
    public ServerResponse<Set<Category>> getChildrenLast(Integer categoryId) {
        Set<Category> set = Sets.newHashSet();
        getChildRecursion2(set, categoryId);
        if (CollectionUtils.isEmpty(set)) set.add(categoryMapper.selectByPrimaryKey(categoryId));
        return ServerResponse.createBySuccess(set);
    }


    //递归查询指定id的所有子品类
    private void getChildRecursion(Set<Category> categorySet, Integer categoryId) {
        List<Category> categories = categoryMapper.selectChildByParentId(categoryId);
        for (Category category : categories) {
            categorySet.add(category);
            getChildRecursion(categorySet, category.getId());
        }
    }

    //递归查询指定id下所有没有子节点的子品类
    private int getChildRecursion2(Set<Category> categorySet, Integer categoryId) {
        List<Category> categories = categoryMapper.selectChildByParentId(categoryId);
        for (Category category : categories) {
            if (getChildRecursion2(categorySet, category.getId()) == 0)
                categorySet.add(category);
        }
        return categories.size();
    }


    @Test
    public void test() {
        ServerResponse<Set<Category>> allChildren = getAllChildren(100031);
        allChildren.getData().forEach(a -> System.out.println(a.toString()));
    }
}